package com.example.locachat;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class TreeDBDao {

        private static final String DB_NAME = "ListData.db";//数据库名称
        private static final String TABLE_NAME = "ListDatainfo";//数据表名称
        private static final int DB_VERSION = 1;//数据库版本

        //表的字段名
        private static String KEY_ID = "id";
        private static String KEY_CONTENT = "name";
        private static String KEY_TYPE = "type";
        private static String KEY_TIME = "time";

        private SQLiteDatabase mDatabase;
        private Context mContext;
        private ListDataDBOpenHelper mDbOpenHelper;//数据库打开帮助类


        public TreeDBDao(Context context) {
            mContext = context;
        }

        //打开数据库
        public void openDataBase() {
            mDbOpenHelper = new ListDataDBOpenHelper(mContext, DB_NAME, null, DB_VERSION);
            try {
                mDatabase = mDbOpenHelper.getWritableDatabase();//获取可写数据库
            } catch (SQLException e) {
                mDatabase = mDbOpenHelper.getReadableDatabase();//获取只读数据库
            }
        }

        //关闭数据库
        public void closeDataBase() {
            if (mDatabase != null) {
                mDatabase.close();
            }
        }

        //插入一条数据
        public long insertData(ListData ListData) {
            ContentValues values = new ContentValues();
            values.put(KEY_CONTENT, ListData.getContent());
            values.put(KEY_TYPE, ListData.getFlag());
            values.put(KEY_TIME, ListData.getTime());
            return mDatabase.insert(TABLE_NAME, null, values);
        }

        //删除一条数据
        public long deleteData(long id) {
            return mDatabase.delete(TABLE_NAME, KEY_ID + "=" + id, null);
        }

        //删除所有数据
        public long deleteAllData() {
            return mDatabase.delete(TABLE_NAME, null, null);
        }

        //更新一条数据
        public long updateData(long id, ListData ListData) {
            ContentValues values = new ContentValues();
            values.put(KEY_CONTENT, ListData.getContent());
            values.put(KEY_TYPE, ListData.getFlag());
            values.put(KEY_TIME, ListData.getTime());
            return mDatabase.update(TABLE_NAME, values, KEY_ID + "=" + id, null);
        }

        //查询一条数据
        public List<ListData> queryData(long id) {
            Cursor results = mDatabase.query(TABLE_NAME, new String[]{KEY_ID, KEY_CONTENT, KEY_TYPE, KEY_TIME},
                    KEY_ID + "=" + id, null, null, null, null);
            return convertToListData(results);
        }

        //查询所有数据
        public List<ListData> queryDataList() {
            Cursor results = mDatabase.query(TABLE_NAME, new String[]{KEY_ID, KEY_CONTENT, KEY_TYPE, KEY_TIME},
                    null, null, null, null, null);
            return convertToListData(results);

        }

        private List<ListData> convertToListData(Cursor cursor) {
            int resultCounts = cursor.getCount();
            if (resultCounts == 0 || !cursor.moveToFirst()) {
                return new ArrayList<>();
            }
            List<ListData> mListDataList = new ArrayList<>();
            for (int i = 0; i < resultCounts; i++) {
                ListData ListData = new ListData();
                ListData.setId(cursor.getInt(0));
                ListData.setContent(cursor.getString(cursor.getColumnIndex(KEY_CONTENT)));
                ListData.setFlag(cursor.getInt(cursor.getColumnIndex(KEY_TYPE)));
                ListData.setTime(cursor.getString(cursor.getColumnIndex(KEY_TIME)));
                mListDataList.add(ListData);
                cursor.moveToNext();
            }
            return mListDataList;
        }

        /**
         * 数据表打开帮助类
         */
        private static class ListDataDBOpenHelper extends SQLiteOpenHelper {

            public ListDataDBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
                super(context, name, factory, version);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                final String sqlStr = "create table if not exists " + TABLE_NAME + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT + " text not null, " + KEY_TYPE + " integer," + KEY_TIME + " text);";
                db.execSQL(sqlStr);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                final String sqlStr = "DROP TABLE IF EXISTS " + TABLE_NAME;
                db.execSQL(sqlStr);
                onCreate(db);
            }
        }
    }